package com.example.demo.controller;

import com.example.demo.dao.BookDao;
import com.example.demo.entity.Book;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Optional;


/*
* 返回模板不能使用@RestController控制器
* */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private BookDao bookDao;
    @ResponseBody
    @RequestMapping("/index")
    public Object list() {
        List<Book> list = bookService.listAll();
        if( list == null ) return "{\"msg\":\"无数据\"}";
        return bookService.listAll();
    }
    @RequestMapping("/view")
    public String view(Model model){
        Optional<Book> book = bookDao.findById(1);
        System.out.println(book.get());
        model.addAttribute("book", book.get());
        model.addAttribute("test","这一个晕啊面");
        return "book/view";
    }
}
